package com.example.easeimpro.controller.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.easeimpro.R;
import com.example.easeimpro.model.bean.PickContactInfo;

import java.util.List;

public class PickContactAdapter extends RecyclerView.Adapter<PickContactAdapter.MyHolder> {

    private Context mContext;
    private List<PickContactInfo> mPickList;

    public PickContactAdapter(Context mContext, List<PickContactInfo> mPicks) {
        this.mContext = mContext;
        if(mPicks!=null&&mPicks.size()>0){
           mPickList = mPicks;
        }

    }
        public List<PickContactInfo> getPickData(){
            return mPickList;
        }
    @NonNull
    @Override
    public PickContactAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyHolder(LayoutInflater.from(mContext).inflate(R.layout.pickcontact_list_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull PickContactAdapter.MyHolder holder, int position) {
        int pos = position;
        holder.pickName.setText(mPickList.get(position).getUser().getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.checkBox.setChecked(!holder.checkBox.isChecked());
                mPickList.get(pos).setChecked(holder.checkBox.isChecked());
            }
        });

    }

    @Override
    public int getItemCount() {
        return mPickList == null ? 0:mPickList.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        private TextView pickName;
        private CheckBox checkBox;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            pickName = itemView.findViewById(R.id.cb_list_name);
            checkBox = itemView.findViewById(R.id.cb_list_item);
        }
    }
}
